package com.zzb.framework;

import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * 框架启动方法
 * Created by zhangzhenbin on 17-5-2.
 */
@SpringBootApplication
@PropertySource({"classpath:framework.properties"})
public class FrameworkMain implements EmbeddedServletContainerCustomizer {
    private static final Logger logger = LoggerFactory.getLogger(FrameworkMain.class);

    public static void main(String[] args) {
        if(ArrayUtils.isEmpty(args)){
            logger.error("using --spring.datasource.url=<jdbcUrl> --spring.datasource.username=<username> --spring.datasource.password=<password>");
            return;
        }
        SpringApplication.run(FrameworkMain.class, args);

    }

    @Override
    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {

    }

    @Bean
    protected ServletContextListener listener() {
        return new ServletContextListener() {

            @Override
            public void contextInitialized(ServletContextEvent sce) {
                logger.info("Framework ServletContext initialized");
            }

            @Override
            public void contextDestroyed(ServletContextEvent sce) {
                logger.info("Framework ServletContext destroyed");
            }

        };
    }
}
